// 服务端地址
var baseUrl = "http://127.0.0.1:9000";

// sa
var sa = {};

// 打开loading
sa.loading = function (msg) {
    layer.closeAll();	// 开始前先把所有弹窗关了
    return layer.msg(msg, {icon: 16, shade: 0.3, time: 1000 * 20, skin: 'ajax-layer-load'});
};

// 隐藏loading
sa.hideLoading = function () {
    layer.closeAll();
};

let redirectWithHeaders = function redirectWithHeaders(url) {
    const response = fetch(url, {
        method: 'GET',
        headers: {
            'X-Requested-With': 'XMLHttpRequest',
            'Authorization': 'Bearer ' + localStorage.getItem('tokenValue')
        },
    });
    if (response.ok) {
        window.location.href = url;
    } else {
        console.error('Failed to get response:', response.statusText);
    }

}

// 封装一下Ajax
sa.ajax = function (url, data, successFn) {
    $.ajax({
        url: url,
        type: "post",
        data: data,
        dataType: 'json',
        headers: {
            'X-Requested-With': 'XMLHttpRequest',
            'Authorization': 'Bearer ' + localStorage.getItem('tokenValue')
        },
        success: function (res) {
            console.log(baseUrl + url + '请求返回数据：', res);
            successFn(res);
        },
        error: function (xhr, type, errorThrown) {
            if (xhr.status == 0) {
                return alert('无法连接到服务器，请检查网络');
            }
            return alert(url + "请求异常：" + JSON.stringify(xhr));
        }
    });
}

// ----------------------------------- 相关事件 -----------------------------------

// 检查当前是否已经登录，如果已登录则直接开始跳转，如果未登录则等待用户输入账号密码
var pData = {
    client: getParam('client', ''),
    redirect: getParam('redirect', ''),
    mode: getParam('mode', '')
};
sa.ajax(baseUrl + "/sso/getRedirectUrl", pData, function (res) {
    console.log('/sso/getRedirectUrl请求', '参数：', pData, ', 请求结果：', res.data);
    if (res.code == 200) {
        // 已登录，并且redirect地址有效，开始跳转
        location.href = res.data;
        // redirectWithHeaders(res.data);
        // $.ajax({
        //     url: res.data,
        //     type: "post",
        //     dataType: 'html',
        //     headers: {
        //         'X-Requested-With': 'XMLHttpRequest',
        //         'Authorization': 'Bearer ' + localStorage.getItem('tokenValue')
        //     },
        //     success: function (res) {
        //         location.href = res.data;
        //     },
        //     error: function (xhr, type, errorThrown) {
        //         return alert(url + "请求异常：" + JSON.stringify(xhr));
        //     }
        // });
    } else if (res.code == 401) {
        console.log('未登录');
    } else {
        layer.alert(res.msg);
    }
})

// 登录
$('.login-btn').click(function () {
    sa.loading("正在登录...");
    // 开始登录
    var data = {
        name: $('[name=name]').val(),
        pwd: $('[name=pwd]').val()
    };
    sa.ajax(baseUrl + "/sso/doLogin", data, function (res) {
        sa.hideLoading();
        if (res.code == 200) {
            layer.msg('doLogin 请求成功后返回的数据：', res.data);
            localStorage.setItem('tokenName', res.data.tokenName);
            localStorage.setItem('tokenValue', res.data.tokenValue);
            layer.msg('登录成功', {anim: 0, icon: 6});
            setTimeout(function () {
                location.reload();
            }, 800);
        } else {
            layer.msg(res.msg, {anim: 6, icon: 2});
        }
    })
});


// 绑定回车事件
$('[name=name],[name=pwd]').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        $('.login-btn').click();
    }
});

// 输入框获取焦点
$("[name=name]").focus();

// 从url中查询到指定名称的参数值
function getParam(name, defaultValue) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == name) {
            return pair[1] + (pair[2] ? '=' + pair[2] : '');
        }
    }
    return (defaultValue === undefined ? null : defaultValue);
}

// 打印信息
var str = "This page is provided by Sa-Token, Please refer to: " + "https://sa-token.cc/";
console.log(str);
